<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;

class Index extends Frontend {

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize() {
        parent::_initialize();
        $this->assign('menu', 1);
    }

    public function index() {
        $page = input('page',1,'intval');
        $taskWhere['task_status']  = array('IN',[3,4]);
        $task_list = \think\Db::name('task')->where($taskWhere)->order('createtime', 'desc')->limit(10)->page($page)->select();

        if(!empty($task_list)){
            foreach ($task_list as &$val){
                $arr_position = [];
                if(!empty($val['push_position'])){
                    $positionList = \think\Db::name('car_position')->where('car_position_id','in',$val['push_position'])->select();
                    foreach($positionList as $vv){
                        array_push($arr_position, $vv['position_name']);
                    }
                }
                $val['xg_img'] = explode(',', $val['xg_img']);
                $val['push_position'] = $arr_position;
                $val['duration'] = $val['sk_end_time'] - $val['sk_start_time'];
                $nowtime = time();
                if($val['get_end_time'] > $nowtime){
                    $timeList = timediff($val['get_end_time'], $nowtime);
                    $val['get_time'] = "{$timeList['day']}天{$timeList['hour']}时{$timeList['min']}分后";
                }else{
                    $val['get_time'] = "0天0时0分后";
                }
            }
            unset($val);
        }
        $bannerList = \think\Db::name('ad')->where('starttime','elt',time())->where('endtime','egt',time())->order('sort ASC, createtime','desc')->select();
        
        $fristImg = '';
        $endImg = '';
        $i = 0;
        
        foreach ($bannerList as $val) {
            if ($i == 0) {
                $fristImg = $val;
            }

            if ($i = count($bannerList) - 1) {
                $endImg = $val;
            }
            $i++;
        }
        
        $this->assign('firstImg', $fristImg);
        $this->assign('endImg', $endImg);
        $this->assign('bannerList', $bannerList);
        $this->assign('list', $task_list);
        
        $this->assign('title', '部落社');
        return $this->view->fetch();
    }

    public function item() {

        $p = input('p', 0);
        $size = input('psize', 10);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }
        $taskWhere['task_status']  = array('IN',[3,4]);
        $task_list = \think\Db::name('task')->where($taskWhere)->order('createtime', 'desc')->limit("$page,$size")->select();
        $this->assign('list', $task_list);
        return $this->view->fetch();
    }

    public function news() {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }
    
    public function detail(){
        $task_id = input('task_id',0,'intval');
        $taskInfo = \think\Db::name('task')->where(['task_id'=>$task_id])->find();
        if(!empty($taskInfo)){
            $arr_position = [];
            if(!empty($taskInfo['push_position'])){
                $positionList = \think\Db::name('car_position')->where('car_position_id','in',$taskInfo['push_position'])->select();
                foreach($positionList as $val){
                    array_push($arr_position, $val['position_name']);
                }
            }
            $taskInfo['push_position'] = $arr_position;
            
           $arr_brand = [];
           if(!empty($taskInfo['car_brand_list'])){
               $brandList = \think\Db::name('car_brand')->where(['car_brand_id'=>array('in',$taskInfo['car_brand_list'])])->select();
               foreach($brandList as $val){
                   array_push($arr_brand, $val['brand_name']);
               }
               $taskInfo['car_brand_list'] = implode(',', $arr_brand);
           }else{
               $taskInfo['car_brand_list'] = '全部';
           }
           
           $arr_color = [];
           if(!empty($taskInfo['car_color_list'])){
               $colorList = \think\Db::name('car_color')->where(['car_color_id'=>array('in',$taskInfo['car_color_list'])])->select();
               foreach($colorList as $val){
                   array_push($arr_color, $val['color_name']);
               }
               $taskInfo['car_color_list'] = implode(',', $arr_color);
           }else{
               $taskInfo['car_color_list'] = '全部';
           }
           
           $arr_type = [];
           if(!empty($taskInfo['car_type_list'])){
               $typeList = \think\Db::name('car_type')->where(['car_type_id'=>array('in',$taskInfo['car_type_list'])])->select();
               foreach($typeList as $val){
                   array_push($arr_type, $val['type_name']);
               }
               $taskInfo['car_type_list'] = implode(',', $arr_type);
           }else{
               $taskInfo['car_type_list'] = '全部';
           }
            $nowtime = time();
            $taskInfo['duration'] = $taskInfo['get_end_time'] - $nowtime;
            $taskInfo['xg_img'] = explode(',', $taskInfo['xg_img']);
            if(count($taskInfo['xg_img']) > 0){
                $taskInfo['xg_img_head'] =  $taskInfo['xg_img'][0];
                $taskInfo['xg_img_end'] =  end($taskInfo['xg_img']);
            }
            

        }
        $carInfo = \think\Db::name('user_car')->where(['user_id'=> $this->iUserId,'is_del'=>0])->find();
        $orderInfo = \think\Db::name('order')->where(['task_id'=>$task_id ,'user_id'=> $this->iUserId,'order_status'=>['neq',90]])->find();
        //网点列表
        $pwhere['is_del'] = 0;
        $pwhere['position_id'] = array('in',$taskInfo['position_ids']);
        $positionList = \think\Db::name('position')->field('position_id as value, position_name as text')->where($pwhere)->order('position_id ASC')->select();

        //平台热线
        $xgpzInfo = \think\Db::name('configure')->where(['key'=>'xiangguanpeizhi'])->find();
        $arrXgpzInfo = json_decode($xgpzInfo['desc'],TRUE);
        
        
        $this->assign('pt_phone', $arrXgpzInfo['pt_phone']);
        $this->assign('carInfo', $carInfo);
        $this->assign('orderInfo', $orderInfo);
        $this->assign('positionList', json_encode($positionList));
        $this->assign('positionCount', count($positionList));
        $this->assign('taskInfo', $taskInfo);
        return $this->view->fetch();
    }
    
    public function addorder(){
        $user_id = $this->iUserId;
        $task_id = input('task_id',0,'intval');
        $position_id = input('position_id',0,'intval');
        if(empty($task_id)){
            $this->error('请选择任务');
        }
        $taskInfo = \think\Db::name('task')->where(['task_id'=>$task_id])->find();
        if($taskInfo['task_num'] <= 0){
            $this->error('该任务已经被抢完，请选择其他任务');
        }
        $nowTime = time();
        if($nowTime < $taskInfo['get_start_time']){
            $this->error('该任务领取时间未开始');
        }
        if($nowTime > $taskInfo['get_end_time']){
            $this->error('该任务领取时间已结束');
        }
        
        $orderInfo = \think\Db::name('order')->where(['user_id'=>$user_id,'task_id'=>$task_id,'order_status'=>['neq',90]])->find();
        if($orderInfo){
            $this->error('任务已抢过');
        }
        
        $data['order_no'] = '1' . time() . mt_rand(1000, 9999);
        $data['task_id'] = $task_id;
        $data['user_id'] = $this->iUserId;
        $data['position_id'] = $position_id;
        $data['createtime'] = $nowTime;
        $data['updatetime'] = $nowTime;
        $res = \think\Db::name('order')->insert($data);
        if($res){
            model('task')->where(['task_id'=>$data['task_id']])->setDec('task_num');
            $this->success('抢单成功，请按照要求完成任务');
        }else{
            $this->error('抢单失败');
        }
    }
    
    //网点列表
    public function wdlist(){
        //网点列表
        $positionList = \think\Db::name('position')->where(['is_del'=>0])->order('position_id ASC')->select();
        $this->assign('positionList', $positionList);
        return $this->view->fetch();
    }

}
